﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Data;

namespace Imgalib.Utility
{
    /// <summary>
    /// Convert To XML format from data collection
    /// </summary>
    public class XmlConverter
    {
        /// <summary>
        /// Convert List of objects to XML
        /// </summary>
        /// <typeparam name="T">Type of object</typeparam>
        /// <param name="listToExport">object list</param>
        /// <returns>xml as string</returns>
        public static String ConvertListToXML<T>(List<T> listToExport)
        {
            StringBuilder formattedXML = new StringBuilder();
            XmlDocument doc = new XmlDocument();
            XmlNode node = doc.CreateNode(XmlNodeType.Element, string.Empty, "root", null);

            foreach (T dataItem in listToExport)
            {
                XmlElement element = doc.CreateElement("data");
                PropertyInfo[] allProperties = dataItem.GetType().GetProperties();
                foreach (PropertyInfo thisProperty in allProperties)
                {
                    object value = thisProperty.GetValue(dataItem, null);
                    XmlElement tmp = doc.CreateElement(thisProperty.Name);
                    if (value != null)
                    {
                        tmp.InnerXml = (RegExUtility.HasSpecialCharacter(value.ToString()) ? @"<![CDATA[" + value.ToString() + "]]>" : value.ToString());
                    }
                    else
                    {
                        tmp.InnerXml = string.Empty;
                    }
                    element.AppendChild(tmp);


                }

                node.AppendChild(element);
            }

            doc.AppendChild(node);

            return doc.InnerXml;
        }

        /// <summary>
        /// Convert DataTable to XML
        /// </summary>
        /// <param name="tableToExport">DataTable to convert </param>
        /// <returns>xml as string</returns>
        public static String ConvertDataTableToXML(DataTable tableToExport)
        {
            StringBuilder formattedXML = new StringBuilder();
            XmlDocument doc = new XmlDocument();
            XmlNode node = doc.CreateNode(XmlNodeType.Element, string.Empty, "root", null);
            DataColumnCollection dtColumns=tableToExport.Columns;

            foreach (DataRow dataItem in tableToExport.Rows)
            {
                XmlElement element = doc.CreateElement("data");
                foreach (DataColumn thisColumn in dtColumns)
                {
                    object value = dataItem[thisColumn];
                    XmlElement tmp = doc.CreateElement(thisColumn.ColumnName);
                    if (value != null)
                    {
                        tmp.InnerXml = (RegExUtility.HasSpecialCharacter(value.ToString()) ? @"<![CDATA[" + value.ToString() + "]]>" : value.ToString());
                    }
                    else
                    {
                        tmp.InnerXml = string.Empty;
                    }

                    element.AppendChild(tmp);
                }

                node.AppendChild(element);
            }

            doc.AppendChild(node);

            return doc.InnerXml;
        }
    }
}
